class Solution:
    def trap(self, height: list[int]) -> int:
        """计算给定高度数组能够接住的雨水量"""
        n = len(height)
        if n == 0:
            return 0

        # 初始化左侧最大值数组
        left_max = [0] * n
        left_max[0] = height[0]
        for i in range(1, n):
            left_max[i] = max(left_max[i - 1], height[i])

        # 初始化右侧最大值数组
        right_max = [0] * n
        right_max[-1] = height[-1]
        for i in range(n - 2, -1, -1):
            right_max[i] = max(right_max[i + 1], height[i])

        # 计算总储水量
        total_water = 0
        for i in range(n):
            total_water += min(left_max[i], right_max[i]) - height[i]

        return total_water